package com.ai.smlcore.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Random;

import sun.misc.BASE64Encoder;

/**
 * web servcie 上传下载文件
 *
 * @author Ray
 *
 */
@SuppressWarnings("restriction")
public class FileProgressUtil {

    /**
     * 上传文件
     *
     * @param fileName
     * @param fileContext
     * @return
     */

    public String uploadFile(String fileName, String fileContext, String cmpgnId) {
        FileOutputStream fos = null;
        try {
            String filedir = getdir();
            String afterEncoding = getBASE64(fileContext);
            if (filedir == "") {
                return "";
            }
            Integer rdm = new Random().nextInt(10000);
            String savename = cmpgnId + "_" + rdm.toString() + fileName.substring(fileName.indexOf('.'));
            fos = new FileOutputStream(filedir + savename);
            // 将字节数组bytes中的数据，写入文件输出流fos中
            fos.write(afterEncoding.getBytes());
            fos.flush();
            fos.close();
            return filedir + savename;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            try {
                if (null != fos) {
                    fos.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 下载文件
     *
     * @param filepath
     * @return
     */
    public String downloadFile(String filepath) {
        // filepath = "F:\\youme\\vedio\\2013-09-03\\201309031700143294.amr";
        FileInputStream in = null;
        byte bytes[] = null;
        String file = null;
        try {
            in = new FileInputStream(filepath);
            int num = 0;
            bytes = new byte[in.available()];
            while ((num = in.read(bytes)) > 0) {
                // 从输入流in中,将 bytes.length 个字节的数据读入字节数组bytes中
                // in.read(bytes);
                BASE64Encoder encoder = new BASE64Encoder();
                file = encoder.encode(bytes);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    /**
     * 获取路径
     *
     * @param filepath
     */
    private String getdir() {
        String path = "C:\\test\\" + getDataString() + "\\";
        try {
            java.io.File file = new java.io.File(path);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    return "";
                }
            }
            return path;
        } catch (Exception ex) {
            return "";
        }
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    private static String getDataString() {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
            return formatter.format(curDate);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return "";
        }
    }

    /**
     * 使用base64加密
     *
     * @param s
     * @return
     */
    private static String getBASE64(String s) {
        if (s == null) {
            return null;
        }
        return new sun.misc.BASE64Encoder().encode(s.getBytes());
    }

    /**
     * 使用base64解密
     *
     * @param s
     * @return
     */
    private static String getFromBASE64(String s) {
        if (s == null) {
            return null;
        }
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(s);
            return new String(b);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        FileProgressUtil f = new FileProgressUtil();
        f.uploadFile("abc.txt", "13899999999,14888111111", "871020");
        System.out.println(FileProgressUtil.getBASE64("13899999999,14888111111"));
        String a = FileProgressUtil.getBASE64("13899999999,14888111111");
        System.out.println(FileProgressUtil.getFromBASE64(a));
    }
}
